package com.example.royalchef.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.royalchef.R;
import com.example.royalchef.utils.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author: Administrator
 * created on: 2017/7/3 9:17
 * description:
 */

public class ShowBogImageActivity extends AppCompatActivity {

    @BindView(R.id.vp_image)
    ViewPager viewPager;
    private ArrayList<String> checkList;
    private int position;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_show_big_image);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        checkList = intent.getStringArrayListExtra("checkList");
        position = intent.getIntExtra("position", 0);
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度（像素）
        int height = metric.heightPixels;   // 屏幕高度（像素）
        MyAdapter adapter = new MyAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(position, true);
    }

    class MyAdapter extends PagerAdapter {

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return checkList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(ShowBogImageActivity.this);
            Picasso.with(ShowBogImageActivity.this)
                    .load("http://192.168.12.248:8080/" + checkList.get(position))
                    .resize(UIUtils.dip2px(60), UIUtils.dip2px(60))
                    .centerCrop()
                    .config(Bitmap.Config.RGB_565)
                    .into(imageView);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setAdjustViewBounds(true);
            container.addView(imageView);
            return imageView;
        }
    }

}
